package cn.comon.app.report;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;

public class AppScanControllerNetqinTest{
	public static void sendRequest(String id, String secureKey) throws IOException{
		URL request = new URL("http://localhost:8080/interfaces/callback.api/netq?sid=20");
		HttpURLConnection connection = (HttpURLConnection)request.openConnection();
		
		//鏋勯�post璇锋眰
		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.setRequestMethod("POST");
		connection.setUseCaches(false);
		connection.setInstanceFollowRedirects(true);
//		connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//		connection.setRequestProperty("content-type", "text/html");
//		connection.setRequestProperty("content-type", "text/json");
//		connection.setConnectTimeout(30000);
//		connection.setReadTimeout(30000); 
		connection.connect();
    
//		String scanParam = "{\"scanlist\":[{\"sid\":\"123\", \"url\":\"http://dl.test.com/test.apk\", \"md5\":\"3d41f29d762ec547bfa4b42f57f3dc7c\"}]}";
//		String scanParam = "{\"account\":\"12321\",\"packageurl\":\"http://www.baidu.com/test.apk\",\"packagename\":\"testapk\",\"packagetype\":1,\"orig\":3,\"clientip\":\"1.2.3.4\",\"appchannel\":\"游戏频道\",\"descr\":\"游戏,广告\"}";
		
		String xml = "<ScanInfo>  <callback>    http://openapp.12321.cn/interfaces/callback.api/netq?sid=156172  </callback>  <authInfo>    <authority>1</authority>  </authInfo>  <pkgInfo>    <appname>91 Launcher</appname>    <platform>apk</platform>    <filesize>7240974</filesize>  </pkgInfo>  <responseInfo>    <security>safe</security>    <virusname>virusname12321</virusname>    <reason>reason12321</reason>  </responseInfo></ScanInfo>";
//      scanParam = URLEncoder.encode("[{\"sid\":\"业务id\",\"md5\":\"文件md5\"}]", "UTF-8");
		
		
		//璁＄畻authkey
//		String authKey = getStringMd5(URLEncoder.encode(scanParam + id + secureKey, "UTF-8"));
//		String authKey = "5b989ab76d0d2a45caff9f5adb2cd96e";
//		scanParam = URLEncoder.encode(scanParam, "UTF-8");
		String post = "callback=" + xml;
//		String post = scanParam;
        System.out.println("<----------------reqeust begin---------------->");
        System.out.println(post);
        System.out.println("<----------------reqeust end---------------->"); 
        
        DataOutputStream writeStream = new DataOutputStream(connection.getOutputStream());
        writeStream.writeBytes(post); 
        writeStream.flush();
        writeStream.close(); 
        
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
        String line="";
        System.out.println("<----------------response begin---------------->");
        while ((line = reader.readLine()) != null){
           //eg:{"result":"error", "reason":"invalid ip"}
            System.out.println(URLDecoder.decode(line, "UTF-8"));
        }
        System.out.println("<----------------response end---------------->");
        reader.close();
        connection.disconnect();
    }	
	
	public static String getStringMd5(String src) {  
		MessageDigest md;  
		StringBuilder md5 = new StringBuilder();
		try{  
			md = MessageDigest.getInstance("md5");  
		    md.update(src.getBytes());  		    
		    for (byte b : md.digest()) {
		    	md5.append(String.format("%02x", b));
		    }
		} 
		catch (Exception e) {  
			e.printStackTrace();  
		}    
		return md5.toString();
	}
	
	public static void main(String[] args) {
	    try {
	    	sendRequest("12321", "OUIaz8gwGQTTb1kZ");
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	}
}